package demo;
class Money implements Cloneable{
    public double m = 12.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{//实现Cloneable接口
    public int id;

    public Money money = new Money();
    @Override
    public String toString() {
        return "Person{" + "id=" + id + "}";
    }

    @Override //不同包中需要通过super访问 所以此时类当中必须重写clone方法
    protected Object clone() throws CloneNotSupportedException { //（异常）
        //return super.clone(); //本质还是调用super.clone
        Person tmp = (Person)super.clone(); //克隆person
        tmp.money = (Money)this.money.clone(); //克隆money

        return tmp; //深拷贝
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException { //声明异常
        Person person = new Person();
        Person person2 = (Person)person.clone();

        System.out.println(person.money.m);
        System.out.println(person2.money.m); //浅拷贝
    }
}
